package en.kaist.cs408.acav;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import en.kaist.cs408.R;
import en.kaist.cs408.acav.CreateClass.NetworkHandler;


public class Home extends Activity implements OnClickListener {
	 
		BaseDeDonnees 		db;
		SQLiteDatabase 		dataBase;
		Context 		context;
		
		// This fonction is the constructor of this activity(view), buttons which will be used in the menu are declared here
		@Override
		public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.home);
	        
	        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	        
	        findViewById(R.id.btn_StartCheck).setOnClickListener(this);
	        findViewById(R.id.btn_checkVoice).setOnClickListener(this);
	        findViewById(R.id.btn_createClass).setOnClickListener(this);
	        findViewById(R.id.btn_debug).setOnClickListener(this);
	        findViewById(R.id.btn_secret).setOnClickListener(this);
	        
//	        db = new BaseDeDonnees(this);
//	        db.open();
//
//	        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
//    		String nomMedecin = preferences.getString("nomMedecin", "");
//    		if (nomMedecin.length() != 0){
//        		Toast.makeText(this, "Bonjour "+ nomMedecin, Toast.LENGTH_LONG).show();
//    		}
	    }
		
		//Menu displayed when you click the option menu button from the cellphone
	    @Override
	    public boolean onCreateOptionsMenu(Menu menu) {    	
	    	menu.add(0,100,0,"About this application");
	    	return true;
	    }

	    //This method allows to perform action regarding the pressed button
	    @SuppressLint("SimpleDateFormat")
		@Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	    	switch(item.getItemId()){

	    	case 100: 
					//about page is displayed
	    			Intent settingsActivity2 = new Intent(Home.this,About.class);
					startActivity(settingsActivity2);
					Toast.makeText(this, "About ...", Toast.LENGTH_SHORT).show();
	    		break;
	    	}
	    	return true;
	    }
    
		//Below are the different call-methods from the different buttons
	    public void onClick(View v) {
			switch (v.getId()) {
			
			// checkVoice Button
			case R.id.btn_checkVoice: 
				Intent settingsActivity = new Intent(Home.this,CheckVoice.class);
				startActivity(settingsActivity);
				break;
				
				//StartCheck Button
			case R.id.btn_StartCheck:
				Intent settingsActivity3 = new Intent(Home.this,StartCheck.class);
				startActivity(settingsActivity3);	
				break;
				
			// CreateClass Button
			case R.id.btn_createClass:
				Intent intent = new Intent(Home.this,CreateClass.class);
				startActivity(intent);
				break;
			case R.id.btn_debug:
				Intent intent1 = new Intent(Home.this,AttendanceList.class);
				startActivity(intent1);
				break;
			
			case R.id.btn_secret:
				AlertDialog.Builder ad = new AlertDialog.Builder(Home.this);
				ad.setTitle("Alert");
				ad.setMessage("Sex?");
				ad.setNegativeButton("No", null);
				
				ad.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						NetworkHandler hn = new NetworkHandler();
						NetworkThread nt = new NetworkThread(0, null, null, null, null, null, null, null, false, hn);
						nt.start();
							
					}
				});
				ad.show();
				
				break;
			}
		}
	}